import { Effect, Reducer } from 'umi';
import {  ProjectListObj, RoleTableListItem, Identity, UserTableListItem } from '../type'

import {
  queryUsers,
  queryList,
  postIdentity,
  queryRoleList,
  postRole,
  queryProjectList,
  postProject, searchUser,
} from '@/services/information';

export interface userManageModelState {
  identitys: Array<Identity>;
  users: Array<UserTableListItem>;
  roles: Array<RoleTableListItem>;
  rolesParentList: Array<RoleTableListItem>;
  projectList: Array<ProjectListObj>;
}

export interface UserModelType {
  namespace: 'userManage';
  state: userManageModelState;
  effects: {
    fetchUsers: Effect;
    searchUsers: Effect;
    fetchIdentityList: Effect;
    submitIdentity: Effect;
    fetchRole: Effect;
    submitRole: Effect;
    fetchProject: Effect;
    submitProject: Effect;
  };
  reducers: {
    save: Reducer;
  };
}

const UserManageModel: UserModelType = {
  namespace: 'userManage',

  state: {
    users: [],
    identitys: [],
    roles: [], // 分配角色列表
    rolesParentList: [], // 分配角色父级列表
    projectList: [], // 项目权限列表
  },

  effects: {
    // 获取表格数据列表
    *fetchUsers(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: { users: response.payload },
      });
    },
    *searchUsers({ payload, callback }, { call }) {
      const response = yield call(searchUser,payload);
      if (callback) callback(response);
    },
    // 身份列表
    *fetchIdentityList(_, { call, put }) {
      const response = yield call(queryList);
      yield put({
        type: 'save',
        payload: { identitys: response.payload },
      });
    },

    // 提交分配身份
    *submitIdentity({ payload, callback }, { call }) {
      const response = yield call(postIdentity, payload);

      if (callback) callback(response);
    },

    // 权限管理-分配角色列表
    *fetchRole(_, { call, put }) {
      const response = yield call(queryRoleList);
      let treeData: Array<ProjectListObj> = [];

      if(response && response.payload){
        // 获取树形结构父级
        response.payload.map((item: RoleTableListItem) => {
          if (JSON.stringify(treeData).indexOf(JSON.stringify(item.project)) === -1) {
            treeData.push(item.project);
          }
        });
      }
      yield put({
        type: 'save',
        payload: { roles: response.payload },
      });
      yield put({
        type: 'save',
        payload: { rolesParentList: treeData },
      });
    },

    // 提交分配角色
    *submitRole({ payload, callback }, { call }) {
      const response = yield call(postRole, payload);

      if (callback) callback(response);
    },

    // 权限管理-项目权限列表
    *fetchProject(_, { call, put }) {
      const response = yield call(queryProjectList);
      yield put({
        type: 'save',
        payload: { projectList: response.payload },
      });
    },

    // 提交项目权限
    *submitProject({ payload, callback }, { call }) {
      const response = yield call(postProject, payload);

      if (callback) callback(response);
    },

  },

  reducers: {
    save (state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default UserManageModel;
